//
// Created by 抑~风 on 2022/12/9.
//

#include "semantic_analysis.h"
#include<string>
#include<iostream>
using namespace  std;
namespace CWJ {
    namespace SEMA_ANALYSIS {
        CWJCompiler::AnnotatedTreePtr CWJCompiler::compile(string script){
            // 词法分析
            const auto & tokens = this->lever.tokenize(script);
            // 语法分析
            const auto &tree = this->parser.parseLL1(tokens);

            this->rootNode = new AnnotatedTree ();
            this->rootNode->setTree(tree);

            tree->showResASTNode(tree,"    ");
            // 语义分析
            return this->compile(tree);
        }

        CWJCompiler::AnnotatedTreePtr CWJCompiler::compile(CWJCompiler::ResASTNodePtr root) {
            TypeAndScopeScanner sc;
            sc.visit(this->rootNode);

            TypeResolver tr;
            tr.visit(this->rootNode);

            RefResolver rr;

            rr.visit(this->rootNode);

            this->rootNode->showScopeTree();

            return this->rootNode;
        }

        void CWJCompiler::execute(AnnotatedTreePtr tree) {
            ASTExecutor executor;
            executor.execute(tree);
        }




    }
}